// pages/login/login.js
const app = getApp()
const $ = app.globalData
Page({
	data: {
		tips_primary: false,
		tips_primary_disabled: 0,
		tips_warn: false,
		thistext: "验证码已下发，请注意查收",
		errortext: "请输入信息",
		zhuceok:false,
		newloginname:'',
		focus: {
			A: false,
			B: false,
			C: false,
			D: false,
			E: false,
			F: false
		},
		input: {
			A: false,
			B: false,
			C: false,
			D: false,
			E: false,
			F: false
		}
	},
	bindBlur: function(e) {
		this.setData({
			keyboard: 0,
			["focus." + e.target.id]: false,
			["input." + e.target.id]: e.detail.value
		});
	},
	zuceok:function(){
		this.setData({
			zhuceok:false
		});
	},
	bindFocus: function(e) {
		if (e.currentTarget.id) {
			wx.pageScrollTo({
				scrollTop: e.currentTarget.id > "B" ? 270 : 140
			});
			this.setData({
				["focus." + e.currentTarget.id]: true
			});
		}
	},
	bindShowTopTips: function(e) {
		if (!this.data.input['B'] || this.data.input['B'][0] != "1" || this.data.input['B'].length != 11) {
			that.setData({
				tips_warn: true,
				errortext: "请输入正确的手机号"
			})
			setTimeout(function() {
				that.setData({
					tips_warn: false
				})
			}, 3000);
		} else {
			$.sendMessage(this.data.input['B'])
			this.setData({
				tips_primary: true
			});
			setTimeout(() => {
				this.setData({
					tips_primary: false
				});
			}, 3000);
			var timestamp = Date.parse(new Date()) / 1000 + 60;
			this.setData({
				tips_primary_disabled: 60
			});
			var interval = setInterval(() => {
				if (timestamp >= Date.parse(new Date()) / 1000) {
					this.setData({
						tips_primary_disabled: timestamp - Date.parse(new Date()) / 1000
					});
				} else {
					this.setData({
						tips_primary_disabled: 0
					});
					clearInterval(interval);
					return true;
				}
			}, 1000);
		}
	},
	// 注册
	register: function() {
		wx.showLoading({
			title: '信息审核中'
		})
		var that = this
		setTimeout(function() {
			var ar = that.data.input
			wx.hideLoading();
			if (!ar['B']) {
				that.setData({
					tips_warn: true,
					errortext: "请输入正确的手机号"
				})
				setTimeout(function() {
					that.setData({
						tips_warn: false
					})
				}, 3000);
			} else if (!ar['C']) {
				that.setData({
					tips_warn: true,
					errortext: "请输入验证码"
				})
				setTimeout(function() {
					that.setData({
						tips_warn: false
					})
				}, 3000);
			} else if (!ar['D']) {
				that.setData({
					tips_warn: true,
					errortext: "请输入密码"
				})
				setTimeout(function() {
					that.setData({
						tips_warn: false
					})
				}, 3000);
			} else if (!ar['E']) {
				that.setData({
					tips_warn: true,
					errortext: "请确认密码"
				})
				setTimeout(function() {
					that.setData({
						tips_warn: false
					})
				}, 3000);
			} else if (ar['E'] != ar['D']) {
				that.setData({
					tips_warn: true,
					errortext: "两次密码输入不一致"
				})
				setTimeout(function() {
					that.setData({
						tips_warn: false
					})
				}, 3000);
			} else if (!ar['F']) {
				that.setData({
					tips_warn: true,
					errortext: "请输入邀请码"
				})
				setTimeout(function() {
					that.setData({
						tips_warn: false
					})
				}, 3000);
			} else {
				wx.showLoading({
					title: '注册中...'
				})
				wx.request({
					url: $.AdminUrl + "/YApi/register",
					data: {
						phone: that.data.input['B'],
						smscode: that.data.input['C'],
						password: that.data.input['D'],
						recommendcode: that.data.input['F']
					},
					success: function(data) {
						wx.hideLoading();
						if (data.data.code) {
							that.setData({
								tips_primary: true,
								thistext: "注册成功",
								zhuceok:true,
								newloginname:data.data.obj.loginname
							})
							setTimeout(function() {
								that.setData({
									tips_primary: false
								})
							}, 3000);
						} else {
							that.setData({
								tips_warn: true,
								errortext: data.data.msg
							})
							setTimeout(function() {
								that.setData({
									tips_warn: false
								})
							}, 3000);
						}
						console.log(data)
					}
				})
			}
		}, 1000)
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// 判断是否已经登陆
		wx.getStorage({
			key: "userinfo",
			success: function(data) {
				console.log(data)
				wx.switchTab({
					url: "../my/my"
				})
			}
		})
		// 获取推荐人id
		var userid = wx.getStorageSync('userid') && wx.getStorageSync('userid') != 'undefined' ? wx.getStorageSync(
			'userid') : false
		var input = this.data.input
		input['F'] = userid
		console.log(userid)
		this.setData({
			input: input
		})
	},
	// 分享转发
	onShareAppMessage: function() {
		var userinfo = wx.getStorageSync('userinfo')
		return {
			title: '优悦购生活管家',
			path: '/pages/register/register?userid=' + userinfo.loginname,
			success: (res) => {
				console.log("转发成功", res);
			},
			fail: (res) => {
				console.log("转发失败", res);
			}
		}
	}
})
